package com.ycsj.goldmedalnewconcept.master.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.master.adapter.RedTemplateAdapter;
import com.ycsj.goldmedalnewconcept.master.bean.RedTemplateInfo;
import com.ycsj.goldmedalnewconcept.view.PullToRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 红包主题
 */
@SuppressLint("NewApi")
public class MasterRedPacageTemplateActivity extends Activity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    //正常加载
    public static final int NORMAL = 0X110;

    //下拉刷新
    public static final int PULLTOREFRESH = 0X120;

    //返回
    private LinearLayout llBack;

    //加载
    private LinearLayout llLoading;

    //空数据
    private LinearLayout llEmpty;

    //刷新控件
    private PullToRefreshLayout ptrl;

    //GridView控件
    private GridView gv;

    //数据集合
    private List<RedTemplateInfo.SkinsBean> oldDatas;

    //屏幕宽度
    private int screenWidth;
    private RedTemplateAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_red_package_template);

        //设置状态栏颜色
        setBar();

        //初始化View
        initViews();

        //设置监听
        initListener();

        //初始化GridView
        initGv();

        //请求服务器获取数据
        http(NORMAL);
    }


    /**
     * 初始化View
     */
    private void initViews() {
        llBack = findViewById(R.id.ll_back);
        llLoading = findViewById(R.id.ll_loading);
        llEmpty = findViewById(R.id.ll_empty);
        ptrl = findViewById(R.id.refresh_view);
        gv = findViewById(R.id.gv);
    }

    /**
     * 设置监听方法
     */
    private void initListener() {
        llBack.setOnClickListener(this);
        ptrl.setOnRefreshListener(this);
    }

    /**
     * 初始化GridView的方法
     */
    private void initGv() {
        //获取屏幕宽度
        //获取屏幕宽度
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        screenWidth = wm.getDefaultDisplay().getWidth();
        oldDatas = new ArrayList<RedTemplateInfo.SkinsBean>();
        adapter = new RedTemplateAdapter(this, oldDatas, R.layout.item_gb_redpackage_template, screenWidth);
    }


    /**
     * 网络请求的方法
     */
    private void http(final int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "11");
        map.put("schoolId", AccountUtils.getSchoolId(this));
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));

        HttpUtils.post(HttpAddress.SENDREDPAG, map, this,
                new HttpCallBack<RedTemplateInfo>() {

                    @Override
                    public void onError(String e) {
                        ToastUtils.showShort("网络错误");
                    }

                    @Override
                    public void onSuccess(RedTemplateInfo info) {
                        try {
                            //请求成功
                            //获取列表数据
                            final List<RedTemplateInfo.SkinsBean> skins = info.getSkins();
                            if (null != skins && skins.size() != 0) {
                                //证明有列表数据,更新主界面
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (method == NORMAL) {
                                            //正常请求
                                            if (llLoading != null) {
                                                llLoading.setVisibility(View.GONE);
                                            }
                                            oldDatas.addAll(skins);
                                            gv.setAdapter(adapter);
                                        } else {
                                            //下拉刷新
                                            oldDatas.clear();
                                            oldDatas.addAll(skins);
                                            ptrl.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
                                            adapter.notifyDataSetChanged();
                                        }
                                    }
                                });
                            } else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        //证明没有数据,显示空视图,隐藏加载框
                                        if (method == NORMAL) {
                                            //正常加载
                                            if (llLoading != null) {
                                                llLoading.setVisibility(View.GONE);
                                            }
                                            if (llEmpty != null) {
                                                llEmpty.setVisibility(View.VISIBLE);
                                            }
                                            gv.setAdapter(adapter);
                                        } else {
                                            //下拉刷新
                                            ptrl.refreshFinish(PullToRefreshLayout.REFRESH_FAIL);
                                        }
                                    }
                                });
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                });
    }

    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                //返回
                finish();
                break;
        }
    }

    /**
     * 下拉刷新控件
     */
    @Override
    public void onRefresh() {
        http(PULLTOREFRESH);
    }


    /**
     * 设置沉浸状态栏的方法,需要在setContentView()之后调用
     */
    private void setBar() {
        //兼容5.0+，状态栏设置透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else
            //为了沉浸式状态栏 4.4以上才有
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
    }

}
